package my.suveng.oauth2.controller;

import my.suveng.model.common.interfaces.response.IMessage;
import my.suveng.model.common.response.Message;
import my.suveng.model.common.response.element.ElementPage;
import my.suveng.model.common.response.element.PageQuery;
import my.suveng.oauth2.model.dto.*;
import my.suveng.oauth2.service.db.mysql.model.entity.SysGroup;
import my.suveng.oauth2.service.db.mysql.model.entity.SysGroupRole;
import my.suveng.oauth2.service.user.impl.GroupServiceImpl;
import my.suveng.oauth2.service.user.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户组管理接口
 *
 * @author suwenguang
 **/
@RestController
@RequestMapping("/api/group")
public class GroupController {
    @Autowired
    private GroupServiceImpl groupService;
    @Autowired
    private UserServiceImpl userService;

    /**
     * 用户组列表
     *
     * @author suwenguang
     */
    @PostMapping("/list")
    public IMessage<ElementPage<List<SysGroup>>> groupList(@RequestBody PageQuery<GroupSearch> query) {
        return groupService.groupList(query);
    }

    /**
     * 增加用户组
     *
     * @author suwenguang
     */
    @PostMapping("/add")
    public IMessage<SysGroup> add(@RequestBody GroupAddDto groupAddDto) {
        // 参数校验
        IMessage<SysGroup> sysGroupMessage = userService.addGroup(groupAddDto);
        if (sysGroupMessage.isSuccess()) {
            return Message.successWithData(sysGroupMessage.getData());
        }
        return Message.error(sysGroupMessage.getMessage());
    }

    /**
     * 修改用户组
     *
     * @author suwenguang
     */
    @PostMapping("/edit")
    public IMessage<SysGroup> edit(@RequestBody GroupEditDto groupEditDto) {
        // 参数校验
        IMessage<SysGroup> edit = groupService.edit(groupEditDto);
        return edit.isSuccess() ? Message.successWithData(edit.getData()) : Message.error(edit.getMessage());
    }

    /**
     * 删除用户组
     *
     * @author suwenguang
     */
    @PostMapping("/delete")
    public IMessage<Object> delete(@RequestBody GroupDeleteDto groupDeleteDto) {
        // 参数校验
        IMessage<Object> delete = groupService.delete(groupDeleteDto);
        return delete.isSuccess() ? Message.successWithData(delete.getData())
                : Message.successWithData(delete.getMessage());
    }

    /**
     * 授予用户组角色
     *
     * @author suwenguang
     */
    @PostMapping("/add/role")
    public IMessage<List<SysGroupRole>> addRole(@RequestBody GroupAddRoleDto groupAddRoleDto) {
        // 参数校验
        IMessage<List<SysGroupRole>> listIMessage = groupService.addRole(groupAddRoleDto);
        return listIMessage.isSuccess() ? Message.successWithData(listIMessage.getData())
                : Message.error(listIMessage.getMessage());
    }
}
